/*
*
* index page script
*
* */
(function ($, window){

    var debounce = utilsLoad.debounce,
        createScript = utilsLoad.createScript;

    // index object
    var thisObj = (function () {

        // 合作单位
        var collegeHtml = '',
            collegeImages = ['college_1.png','college_2.png','college_3.png','college_4.png','college_5.png','college_6.png'];
        collegeHtml += collegeImages.reduce(function (prev, curr) {
            return prev + '<div class="slider-item"><img src="/public/images/index/'+ curr +'" alt=""></div>';
        }, '');
        $('.cooperSlider>div').html(collegeHtml);


        // binding layui.form
        // 搜索职位
        layui.form.on('submit(search_post)', function(data){
            var post = data.field.searchPost;
            if(!post.trim()){
                layer.msg('请输入搜索关键字', {icon: 5});
                return false
            }
            window.open('/post?wd='+post, '_blank')
        });


        // 热搜词
        var hotSearchWords = function () {
            var html = '';

            $.ajax({
                url: API_BASE_URL + '/userCenter/listHotwords'
            }).done(function (res) {
                if(res.message === 'success' && res.data && res.data.length){
                    html += `<span>热门搜索：</span>`;
                    res.data = res.data.slice(0, 4);
                    res.data.forEach(word => {
                        html += `<a href="/post?wd=${word}" target="_blank">${word}</a>`
                    });
                    $('.searchHot').html(html)
                }
            })
        };

        return {
            hotSearchWords
        }

    })();


    /*
    *  配置数据
    * */
    window.onload = function () {

        thisObj.hotSearchWords();

        // 判断是否从修改密码跳转，弹出登录框
        if(window.location.hash === '#type=login'){
            $('.signModal').eq(0).click();
            window.location.hash = ''
        }

        // carousel
        layui.use('carousel', function(){
            var carousel = layui.carousel;
            //建造实例
            carousel.render({
                elem: '.carousel'
                ,width: '100%'
                ,height: '250'
            })
        });

        // 引入插件
        createScript({
            src: '/public/js/plugin.js'
        }, function (){
            // slider
            pluginLoad.rollSlider({
                el: '.cooperSlider',
                speed: 0.8
            })
        });

        // body元素添加mousemove事件代理，筛选职位
        var $filterPost = $('.showFilterPost'),
            $filterAsideMenu = $('.filterAsideMenu'),
            $filterDivMenuItem = $('.filterDivMenu>.item'),
            $filterDivCntItem = $('.filterCnt>.item');
        var $currHoverItem = null,
            $currHoverItemCnt = null,
            currIndex = 0,
            perHeight = 48;
        $('body').on('mousemove', debounce(function (e){
            var $tar = $(e.target);
            // filterDivMenu > item 添加hover
            if($tar.hasClass('item') || $tar.parent('.item').length){
                if($tar.parents('.filterDivMenu').length){

                    $currHoverItem = $tar.hasClass('item') ? $tar : $tar.parent();

                    //已添加hover后，不再执行
                    if($currHoverItem.hasClass('hover')){
                        return false
                    }

                    $filterDivMenuItem.removeClass('hover');
                    $currHoverItem.addClass('hover');

                    currIndex = $currHoverItem.index();

                    $currHoverItemCnt = $filterDivCntItem.eq(currIndex);
                    $filterDivCntItem.removeClass('hover');
                    $currHoverItemCnt.addClass('hover');

                    // 若已设置过top，则不再计算
                    if(!$currHoverItemCnt.attr('style')){
                        $currHoverItemCnt.css({
                            top: 5 + perHeight * currIndex + 'px'
                        })
                    }
                }
            }
            // 隐藏hover菜单
            if(!$tar.parents('.filterCnt').length && !$tar.parents('.filterDivMenu').length){
                $filterDivMenuItem.removeClass('hover');
                $filterDivCntItem.removeClass('hover');
                currIndex = 0;
            }
            // 显示全部职位
            if($tar.hasClass('showFilterPost')){
                $filterPost.addClass('hidden');
                $filterAsideMenu.removeClass('filter-menu-hidden');
            }
            // 隐藏全部职位
            if(!$tar.hasClass('showFilterPost') && !$tar.parents('.filterDivMenu').length){
                // 若hover index 大于4，此时展开全部职位，则不隐藏
                if(currIndex > 4){
                    return false
                }
                $filterPost.removeClass('hidden');
                $filterAsideMenu.addClass('filter-menu-hidden');
            }
        }, 50))
    }

})(jQuery, window);